package com.demo.crm.springboot3security.controller;

import com.demo.crm.springboot3security.dto.FormLayoutConfig;
import com.demo.crm.springboot3security.entity.CustomForm;
import com.demo.crm.springboot3security.service.FormLayoutService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 表单布局配置控制器
 */
@Tag(name = "表单布局配置", description = "表单布局配置相关接口")
@RestController
@RequestMapping("/api/forms/{formId}/layout")
@RequiredArgsConstructor
public class FormLayoutController {

    private final FormLayoutService layoutService;

    @Operation(summary = "获取表单布局配置")
    @GetMapping
    @PreAuthorize("hasAuthority('form:layout:read')")
    public ResponseEntity<FormLayoutConfig> getLayoutConfig(@PathVariable Long formId) {
        return ResponseEntity.ok(layoutService.getLayoutConfig(formId));
    }

    @Operation(summary = "保存表单布局配置")
    @PutMapping
    @PreAuthorize("hasAuthority('form:layout:update')")
    public ResponseEntity<CustomForm> saveLayoutConfig(
            @PathVariable Long formId,
            @RequestBody FormLayoutConfig config) {
        return ResponseEntity.ok(layoutService.saveLayoutConfig(formId, config));
    }
}